#!/bin/bash
#
# container-run.sh -- lab specific startup script,
#
# Copyright (C) 2016-2021 Wu Zhangjin <falcon@ruma.tech>
#

TOP_DIR=$(cd $(dirname $0) && pwd)/

[ ! -d /proc ] && mount -t proc none /proc

# nfsd.ko must be inserted to enable nfs kernel server
grep -qi intel /proc/cpuinfo
if [ $? -eq 0 ]; then
  kvm_dri=kvm-intel
else
  kvm_dri=kvm-amd
fi

# get host os name
os=$HOST_OS
[ -z "$os" ] && os=$(uname)

# FIXME: with linux kernel v5.3-v5.7, nfsd has bug while be plugged in
# container side, but ok in host side, move it back to host side:
# https://gitee.com/tinylab/linux-lab/issues/I1GGG1
# https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?h=v5.10-rc1&id=681370f4b00af0fcc65bbfb9f82de526ab7ceb0a
#
# This may break nfsboot in Windows 10 and Mac OSX, there is no suitable method
# to insert nfsd in 'host' side.

# The nfs module may have been compiled in or have been inserted in host side
if [ ! -d /proc/fs/nfsd -o ! -f /proc/fs/nfs/exports ]; then
  if [ "$os" = "Linux" ]; then
    NFS=nfsd
    major_version=$(uname -r | cut -d '.' -f1)
    minor_version=$(uname -r | cut -d '.' -f2)
    if [ $major_version -eq 5 -a $minor_version -ge 3 -a $minor_version -le 7 ]; then
      NFS=
    fi
  else
    # Insert nfsd in container side for Windows and Mac OS X currently for there is
    # no method to insert nfs module.
    #
    # If they use the same kernel, it should crash too ...
    NFS=nfsd
  fi
fi

modules="loop minix $NFS 9pnet_virtio kvm $kvm_dri"

container_mdir=/m/lib/modules/`uname -r`/
if [ -d $container_mdir -a ! -f $container_mdir/modules.dep.bin ]; then
  depmod -A
fi

for m in $modules
do
  modprobe -d /m -n $m >/dev/null 2>&1
  [ $? -ne 0 ] && continue

  lsmod | grep -q $m
  if [ $? -ne 0 ]; then
    modprobe -d /m $m >/dev/null 2>&1
  fi

  # tell user enable cpu virtualization in bios if not disabled
  if [ $? -ne 0 ]; then
    if [ "$m" = "kvm-intel" -o "$m" = "kvm-amd" ]; then
      echo "LOG: Please turn on cpu virtualization in bios if want use kvm speedup"
    fi
  fi
done

# Configure exports for Windows wsl2 and boot2docker
# Both of them can not export /labs/linux-lab, but can export the ram cached /labs/linux-lab/build
nfs_exports=/etc/exports
if [ -f $nfs_exports ]; then
  if [ "$os" = "Windows" ]; then
    uname -r | grep -E -i "wsl2|boot2docker"
    [ $? -eq 0 ] && sed -i -e '/^\/labs\/linux-lab\/ /s/\(.*\)/#\1/g' $nfs_exports
  else
    sed -i -e '/^#\/labs\/linux-lab\/ /s/#\(.*\)/\1/g' $nfs_exports
  fi
fi

# For qemu-mips64el not supported by old qemu-user-binfmt
[ ! -f /proc/sys/fs/binfmt_misc/regisiter ] && mount -t binfmt_misc none /proc/sys/fs/binfmt_misc
if [ ! -f /proc/sys/fs/binfmt_misc/qemu-mips64el -a -f /proc/sys/fs/binfmt_misc/register ]; then
  sh -c "echo ':qemu-mips64el:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x08\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-mips64el:' > /proc/sys/fs/binfmt_misc/register"
fi

# Configure for core-dump
# echo "/tmp/cores/core.%e.%p" > /proc/sys/kernel/core_pattern
echo core > /proc/sys/kernel/core_pattern

# Enable network bridge support

$TOP_DIR/enable_net_bridge.sh

## Start Net servers
NFS_PATH=/labs/linux-lab/build
if [ ! -d $NFS_PATH ]; then
  mkdir -p $NFS_PATH
  [ -z "$UNIX_USER" ] && UNIX_USER=ubuntu
  chown $UNIX_USER:$UNIX_USER $NFS_PATH
fi
$TOP_DIR/restart-net-servers.sh

## Install aarch64-linux-gnu-gcc alternatives
[ -f /usr/bin/aarch64-linux-gnu-gcc-4.8 ] && \
  update-alternatives --install /usr/bin/aarch64-linux-gnu-gcc aarch64-linux-gnu-gcc /usr/bin/aarch64-linux-gnu-gcc-4.8 45
[ -f /usr/local/bin/aarch64-linux-gnu-gcc-4.9.3 ] && \
  ln -sf /usr/local/bin/aarch64-linux-gnu-gcc-4.9.3 /usr/bin/aarch64-linux-gnu-gcc-4.9.3 && \
  update-alternatives --install /usr/bin/aarch64-linux-gnu-gcc aarch64-linux-gnu-gcc /usr/bin/aarch64-linux-gnu-gcc-4.9.3 46

# FIXME: Disable git detachedHead advice, bsp submodule use FETCH_HEAD currently.
git config --global advice.detachedHead false

# Disable https ssl verify
git config --global http.sslVerify false

# Clean up the target directory
rm -rf /binutils-arm-linux-gnueabi.tar.gz &
